{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <center>超级码力笔试第二题</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div align=\"left\">\n",
    "    <img src=\"https://raw.githubusercontent.com/huber-yaoer/resume/master/01-%E8%B6%85%E7%BA%A7%E7%A0%81%E5%8A%9B%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AF%95/super2.png\"  alt=\"超级码力笔试第二题\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### A->label :   P(A,label)/P(A)=num(A,label)/num(A)  > CONFIDENCE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [],
   "source": [
    "SUPPORT = 0.1\n",
    "CONFIDENCE = 0.7 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 理论：\n",
    "Apriori算法原理总结: https://www.cnblogs.com/pinard/p/6293298.html \n",
    "\n",
    "FP Tree算法原理总结（也称FP Growth算法）: https://www.cnblogs.com/pinard/p/6307064.html \n",
    "\n",
    "Apriori算法： 空间复杂度低，时间复杂度高\n",
    "FP Growth算法：空间复杂度高，时间复杂度低"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 搜集开源工具：\n",
    "### pyfpgrowth\n",
    "https://github.com/evandempsey/fp-growth\n",
    "\n",
    "https://fp-growth.readthedocs.io/en/latest/\n",
    "    \n",
    "### pyspark\n",
    "https://www.cnblogs.com/pinard/p/6340162.html\n",
    "\n",
    "https://spark.apache.org/docs/2.1.1/api/python/pyspark.mllib.html#pyspark.mllib.fpm.FPGrowth \n",
    "\n",
    "## 没有集群，用单机版的spark或虚拟机版的spark在这儿都不能发挥集群的优势\n",
    "## 还是受到单机的限制，所以初步选择python3+pyfpgrowth"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# fpgrowth思路大体分六步：\n",
    "### 1.观察数据，对数据预处理\n",
    "### 2.将数据转化为pyfpgrowth接受的形式\n",
    "### 3.根据支持度生成频繁项集\n",
    "### 4.根据置信度生成规则\n",
    "### 5.从规则中过滤出自己需要的规则\n",
    "### 6.解析规则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 结果，在构建FP Tree时内存爆了：\n",
    "\n",
    "<div align=\"left\">\n",
    "    <img src=\"https://raw.githubusercontent.com/huber-yaoer/resume/master/01-%E8%B6%85%E7%BA%A7%E7%A0%81%E5%8A%9B%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AF%95/super2_FP.png\"  alt=\"超级码力笔试第二题fpgrowth思路结果\" />\n",
    "</div>\n",
    "\n",
    "### 想想也对，特征之间要各种组合，n!级别空间复杂度："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 为了优化，想方设法减小FP Tree，怎么减？\n",
    "#### 不能减样本，会导致结果不准确。\n",
    "#### 只能减特征，根据支持度和置信度条件减了特征，还是不行，内存使用率会慢慢到100%。。。。。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 再审题，发现rule左侧只需要少于等于两项，手写算法，代码行数不会很多。\n",
    "## 所以干脆转换为Apriori算法思路："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Apriori思路大体分三步：\n",
    "### 1.观察数据，对数据预处理\n",
    "### 2.对于A->Label: 根据支持度和置信度条件删除dataframe中一部分特征\n",
    "### 3.对于AB->Label: 根据支持度和置信度条件，直接得出结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 观察数据，观察数据，对数据预处理："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VTYPE_1</th>\n",
       "      <th>VTYPE_2</th>\n",
       "      <th>VTYPE_3</th>\n",
       "      <th>VTYPE_4</th>\n",
       "      <th>VTYPE_5</th>\n",
       "      <th>VSEX_-</th>\n",
       "      <th>VSEX_F</th>\n",
       "      <th>VSEX_M</th>\n",
       "      <th>VAGE_1</th>\n",
       "      <th>VAGE_2</th>\n",
       "      <th>...</th>\n",
       "      <th>LIGHTING_B</th>\n",
       "      <th>LIGHTING_C</th>\n",
       "      <th>LIGHTING_D</th>\n",
       "      <th>LIGHTING_E</th>\n",
       "      <th>RIGHTWAY_-</th>\n",
       "      <th>RIGHTWAY_A</th>\n",
       "      <th>RIGHTWAY_B</th>\n",
       "      <th>RIGHTWAY_C</th>\n",
       "      <th>RIGHTWAY_D</th>\n",
       "      <th>Label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 671 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   VTYPE_1  VTYPE_2  VTYPE_3  VTYPE_4  VTYPE_5  VSEX_-  VSEX_F  VSEX_M  \\\n",
       "0        0        1        0        0        0       0       1       0   \n",
       "1        0        0        1        0        0       0       0       1   \n",
       "2        0        1        0        0        0       0       0       1   \n",
       "3        0        1        0        0        0       0       0       1   \n",
       "4        0        1        0        0        0       0       1       0   \n",
       "\n",
       "   VAGE_1  VAGE_2  ...    LIGHTING_B  LIGHTING_C  LIGHTING_D  LIGHTING_E  \\\n",
       "0       1       0  ...             0           0           0           0   \n",
       "1       0       0  ...             0           0           0           0   \n",
       "2       0       0  ...             0           0           0           0   \n",
       "3       0       0  ...             0           0           0           0   \n",
       "4       1       0  ...             0           1           0           0   \n",
       "\n",
       "   RIGHTWAY_-  RIGHTWAY_A  RIGHTWAY_B  RIGHTWAY_C  RIGHTWAY_D  Label  \n",
       "0           0           1           0           0           0  False  \n",
       "1           0           1           0           0           0   True  \n",
       "2           0           1           0           0           0  False  \n",
       "3           0           1           0           0           0  False  \n",
       "4           0           1           0           0           0  False  \n",
       "\n",
       "[5 rows x 671 columns]"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读数据\n",
    "import pandas as pd \n",
    "#df=pd.read_csv(r'/home/yzh/data/Test2_Data.csv') \n",
    "df=pd.read_csv(r'./Test2_Data.csv') \n",
    "#df=df.sample(frac=0.5, replace=False)#replace=True时为有放回抽样\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VTYPE_1</th>\n",
       "      <th>VTYPE_2</th>\n",
       "      <th>VTYPE_3</th>\n",
       "      <th>VTYPE_4</th>\n",
       "      <th>VTYPE_5</th>\n",
       "      <th>VSEX_-</th>\n",
       "      <th>VSEX_F</th>\n",
       "      <th>VSEX_M</th>\n",
       "      <th>VAGE_1</th>\n",
       "      <th>VAGE_2</th>\n",
       "      <th>...</th>\n",
       "      <th>LIGHTING_A</th>\n",
       "      <th>LIGHTING_B</th>\n",
       "      <th>LIGHTING_C</th>\n",
       "      <th>LIGHTING_D</th>\n",
       "      <th>LIGHTING_E</th>\n",
       "      <th>RIGHTWAY_-</th>\n",
       "      <th>RIGHTWAY_A</th>\n",
       "      <th>RIGHTWAY_B</th>\n",
       "      <th>RIGHTWAY_C</th>\n",
       "      <th>RIGHTWAY_D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.0</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "      <td>1200.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.235833</td>\n",
       "      <td>0.582500</td>\n",
       "      <td>0.161667</td>\n",
       "      <td>0.020000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.009167</td>\n",
       "      <td>0.395833</td>\n",
       "      <td>0.595000</td>\n",
       "      <td>0.245000</td>\n",
       "      <td>0.238333</td>\n",
       "      <td>...</td>\n",
       "      <td>0.547500</td>\n",
       "      <td>0.028333</td>\n",
       "      <td>0.340000</td>\n",
       "      <td>0.074167</td>\n",
       "      <td>0.007500</td>\n",
       "      <td>0.002500</td>\n",
       "      <td>0.390833</td>\n",
       "      <td>0.001667</td>\n",
       "      <td>0.000833</td>\n",
       "      <td>0.604167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.424696</td>\n",
       "      <td>0.493352</td>\n",
       "      <td>0.368298</td>\n",
       "      <td>0.140058</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.095343</td>\n",
       "      <td>0.489233</td>\n",
       "      <td>0.491097</td>\n",
       "      <td>0.430267</td>\n",
       "      <td>0.426242</td>\n",
       "      <td>...</td>\n",
       "      <td>0.497946</td>\n",
       "      <td>0.165993</td>\n",
       "      <td>0.473906</td>\n",
       "      <td>0.262151</td>\n",
       "      <td>0.086313</td>\n",
       "      <td>0.049958</td>\n",
       "      <td>0.488141</td>\n",
       "      <td>0.040808</td>\n",
       "      <td>0.028868</td>\n",
       "      <td>0.489233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 670 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           VTYPE_1      VTYPE_2      VTYPE_3      VTYPE_4  VTYPE_5  \\\n",
       "count  1200.000000  1200.000000  1200.000000  1200.000000   1200.0   \n",
       "mean      0.235833     0.582500     0.161667     0.020000      0.0   \n",
       "std       0.424696     0.493352     0.368298     0.140058      0.0   \n",
       "min       0.000000     0.000000     0.000000     0.000000      0.0   \n",
       "25%       0.000000     0.000000     0.000000     0.000000      0.0   \n",
       "50%       0.000000     1.000000     0.000000     0.000000      0.0   \n",
       "75%       0.000000     1.000000     0.000000     0.000000      0.0   \n",
       "max       1.000000     1.000000     1.000000     1.000000      0.0   \n",
       "\n",
       "            VSEX_-       VSEX_F       VSEX_M       VAGE_1       VAGE_2  \\\n",
       "count  1200.000000  1200.000000  1200.000000  1200.000000  1200.000000   \n",
       "mean      0.009167     0.395833     0.595000     0.245000     0.238333   \n",
       "std       0.095343     0.489233     0.491097     0.430267     0.426242   \n",
       "min       0.000000     0.000000     0.000000     0.000000     0.000000   \n",
       "25%       0.000000     0.000000     0.000000     0.000000     0.000000   \n",
       "50%       0.000000     0.000000     1.000000     0.000000     0.000000   \n",
       "75%       0.000000     1.000000     1.000000     0.000000     0.000000   \n",
       "max       1.000000     1.000000     1.000000     1.000000     1.000000   \n",
       "\n",
       "          ...        LIGHTING_A   LIGHTING_B   LIGHTING_C   LIGHTING_D  \\\n",
       "count     ...       1200.000000  1200.000000  1200.000000  1200.000000   \n",
       "mean      ...          0.547500     0.028333     0.340000     0.074167   \n",
       "std       ...          0.497946     0.165993     0.473906     0.262151   \n",
       "min       ...          0.000000     0.000000     0.000000     0.000000   \n",
       "25%       ...          0.000000     0.000000     0.000000     0.000000   \n",
       "50%       ...          1.000000     0.000000     0.000000     0.000000   \n",
       "75%       ...          1.000000     0.000000     1.000000     0.000000   \n",
       "max       ...          1.000000     1.000000     1.000000     1.000000   \n",
       "\n",
       "        LIGHTING_E   RIGHTWAY_-   RIGHTWAY_A   RIGHTWAY_B   RIGHTWAY_C  \\\n",
       "count  1200.000000  1200.000000  1200.000000  1200.000000  1200.000000   \n",
       "mean      0.007500     0.002500     0.390833     0.001667     0.000833   \n",
       "std       0.086313     0.049958     0.488141     0.040808     0.028868   \n",
       "min       0.000000     0.000000     0.000000     0.000000     0.000000   \n",
       "25%       0.000000     0.000000     0.000000     0.000000     0.000000   \n",
       "50%       0.000000     0.000000     0.000000     0.000000     0.000000   \n",
       "75%       0.000000     0.000000     1.000000     0.000000     0.000000   \n",
       "max       1.000000     1.000000     1.000000     1.000000     1.000000   \n",
       "\n",
       "        RIGHTWAY_D  \n",
       "count  1200.000000  \n",
       "mean      0.604167  \n",
       "std       0.489233  \n",
       "min       0.000000  \n",
       "25%       0.000000  \n",
       "50%       1.000000  \n",
       "75%       1.000000  \n",
       "max       1.000000  \n",
       "\n",
       "[8 rows x 670 columns]"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "#判断是否有缺失值\n",
    "#df.iloc[1:3,1] = np.nan\n",
    "print(True in df.isnull().any().tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Lable列布尔转int\n",
    "df['Label'] = df['Label'].astype('int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1200, 671)"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 下两个cell是为了摸索一下pandas，可运行，不影响挖掘主干："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Label\n",
      "0    600\n",
      "1    600\n",
      "Name: Label, dtype: int64\n",
      "VTYPE_5\n",
      "0    1200\n",
      "Name: VTYPE_5, dtype: int64\n",
      "\n",
      "<class 'pandas.core.series.Series'>\n",
      "600\n",
      "\n",
      "MultiIndex(levels=[[0, 1], [0, 1]],\n",
      "           labels=[[0, 1, 1], [0, 0, 1]],\n",
      "           names=['Label', 'VTYPE_1'])\n",
      "\n",
      "FrozenNDArray([0, 1, 2], dtype='int8')\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Label  VTYPE_1\n",
       "0      0          600\n",
       "1      0          317\n",
       "       1          283\n",
       "Name: Label, dtype: int64"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(df['Label'].groupby(df['Label']).count())\n",
    "print(df['VTYPE_5'].groupby(df['VTYPE_5']).count())\n",
    "print()\n",
    "\n",
    "n_A_and_label = df['Label'].groupby([df['Label'], df['VTYPE_1']]).count()\n",
    "print(type(n_A_and_label))\n",
    "print(n_A_and_label[0][0])\n",
    "print()\n",
    "print(n_A_and_label.index)\n",
    "print()\n",
    "print(n_A_and_label.index.labels[0] + n_A_and_label.index.labels[1])\n",
    "print(2 in (n_A_and_label.index.labels[0] + n_A_and_label.index.labels[1])) #是否存在Label为1，VTYPE_5也为1\n",
    "n_A_and_label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MultiIndex(levels=[[0, 1], [0]],\n",
      "           labels=[[0, 1], [0, 0]],\n",
      "           names=['Label', 'VTYPE_5'])\n",
      "FrozenNDArray([0, 1], dtype='int8')\n",
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Label  VTYPE_5\n",
       "0      0          600\n",
       "1      0          600\n",
       "Name: Label, dtype: int64"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_A_and_label = df['Label'].groupby([df['Label'], df['VTYPE_5']]).count()\n",
    "print(n_A_and_label.index)\n",
    "print(n_A_and_label.index.labels[0] + n_A_and_label.index.labels[1])\n",
    "print(2 in (n_A_and_label.index.labels[0] + n_A_and_label.index.labels[1]))\n",
    "n_A_and_label"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 对于A->Label:  根据支持度和置信度条件删除一部分特征"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n_SUPPORT: 120.0\n",
      "\n",
      "VTYPE_1\n",
      "0    917\n",
      "1    283\n",
      "Name: Label, dtype: int64\n",
      "(1200, 33)\n"
     ]
    }
   ],
   "source": [
    "list_dfcolname = df.columns.values.tolist()\n",
    "print('n_SUPPORT:', df.shape[0] * SUPPORT)\n",
    "print()\n",
    "n_A = df['Label'].groupby(df['VTYPE_1']).count()\n",
    "print(n_A)\n",
    "# print(1 in n_A.index)\n",
    "# n_A = df['Label'].groupby(df['VTYPE_5']).count()\n",
    "# print(1 in n_A.index)\n",
    "for i in range(df.shape[1]):\n",
    "    n_A = df['Label'].groupby(df[list_dfcolname[i]]).count()\n",
    "    n_A_and_label = df['Label'].groupby([df['Label'], df[list_dfcolname[i]]]).count()\n",
    "    if 1 in n_A.index:\n",
    "        #对于A->Label:  根据支持度和置信度条件删除一部分特征\n",
    "        if n_A[1] <= df.shape[0] * SUPPORT:\n",
    "            df.drop([list_dfcolname[i]],axis=1,inplace=True)\n",
    "        else:\n",
    "            if 2 in (n_A_and_label.index.labels[0] + n_A_and_label.index.labels[1]): #是否存在Label为1，A也为1\n",
    "                if n_A_and_label[1][1]/n_A[1] <= CONFIDENCE:#num(A,label)/num(A) < CONFIDENCE:\n",
    "                    df.drop([list_dfcolname[i]],axis=1,inplace=True)\n",
    "    else:\n",
    "        #特征全为零，删除\n",
    "        df.drop([list_dfcolname[i]],axis=1,inplace=True)\n",
    "print(df.shape)                  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 接下来找出规则左侧两项的情况，交叉组合后总的判断次数为 32的平方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['VTYPE_1', 'VTYPE_3', 'VSEAT_1', 'VSEAT_9', 'VSAFETY1_-', 'VSAFETY1_L', 'VSAFETY2_-', 'VEJECTED_1', 'VEJECTED_3', 'PTYPE_2', 'PAGE_6', 'PSOBER_B', 'PDRUG_E', 'PSAFETY1_-', 'PSAFETY1_P', 'PSAFETY2_-', 'INSURED_N', 'INSURED_O', 'CELL_-', 'PVIOLCOD_ ', 'OAF1_A', 'MOVEMENT_-', 'VEHYEAR_2012', 'VEHMAKE_-', 'VEHTYPE_N', 'CHPTYPE1_60', 'TIMECAT_300', 'CRASHTYP_E', 'CRASHTYP_G', 'INVOLVE_B', 'INVOLVE_I', 'POP_9']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['VTYPE_1',\n",
       " {'VSEAT_1', 'VTYPE_1'},\n",
       " 'VTYPE_3',\n",
       " {'VSEAT_9', 'VTYPE_3'},\n",
       " {'VSAFETY1_-', 'VTYPE_3'},\n",
       " {'VSAFETY2_-', 'VTYPE_3'},\n",
       " {'VEJECTED_3', 'VTYPE_3'},\n",
       " {'PTYPE_2', 'VTYPE_3'},\n",
       " {'PSAFETY1_-', 'VTYPE_3'},\n",
       " {'PSAFETY2_-', 'VTYPE_3'},\n",
       " {'INSURED_O', 'VTYPE_3'},\n",
       " {'VEHYEAR_2012', 'VTYPE_3'},\n",
       " {'VEHMAKE_-', 'VTYPE_3'},\n",
       " {'VEHTYPE_N', 'VTYPE_3'},\n",
       " {'CHPTYPE1_60', 'VTYPE_3'},\n",
       " {'CRASHTYP_G', 'VTYPE_3'},\n",
       " {'INVOLVE_B', 'VTYPE_3'},\n",
       " {'VSEAT_1', 'VTYPE_1'},\n",
       " 'VSEAT_1',\n",
       " {'VEJECTED_1', 'VSEAT_1'},\n",
       " {'VSEAT_9', 'VTYPE_3'},\n",
       " 'VSEAT_9',\n",
       " {'VSAFETY1_-', 'VSEAT_9'},\n",
       " {'VSAFETY2_-', 'VSEAT_9'},\n",
       " {'VEJECTED_3', 'VSEAT_9'},\n",
       " {'PTYPE_2', 'VSEAT_9'},\n",
       " {'PSAFETY1_-', 'VSEAT_9'},\n",
       " {'PSAFETY2_-', 'VSEAT_9'},\n",
       " {'INSURED_O', 'VSEAT_9'},\n",
       " {'VEHYEAR_2012', 'VSEAT_9'},\n",
       " {'VEHMAKE_-', 'VSEAT_9'},\n",
       " {'VEHTYPE_N', 'VSEAT_9'},\n",
       " {'CHPTYPE1_60', 'VSEAT_9'},\n",
       " {'CRASHTYP_G', 'VSEAT_9'},\n",
       " {'INVOLVE_B', 'VSEAT_9'},\n",
       " {'VSAFETY1_-', 'VTYPE_3'},\n",
       " {'VSAFETY1_-', 'VSEAT_9'},\n",
       " 'VSAFETY1_-',\n",
       " {'VSAFETY1_-', 'VSAFETY2_-'},\n",
       " {'VEJECTED_3', 'VSAFETY1_-'},\n",
       " {'PTYPE_2', 'VSAFETY1_-'},\n",
       " {'PSAFETY1_-', 'VSAFETY1_-'},\n",
       " {'PSAFETY2_-', 'VSAFETY1_-'},\n",
       " {'INSURED_O', 'VSAFETY1_-'},\n",
       " {'VEHYEAR_2012', 'VSAFETY1_-'},\n",
       " {'VEHMAKE_-', 'VSAFETY1_-'},\n",
       " {'VEHTYPE_N', 'VSAFETY1_-'},\n",
       " {'CHPTYPE1_60', 'VSAFETY1_-'},\n",
       " {'CRASHTYP_G', 'VSAFETY1_-'},\n",
       " {'INVOLVE_B', 'VSAFETY1_-'},\n",
       " 'VSAFETY1_L',\n",
       " {'VSAFETY2_-', 'VTYPE_3'},\n",
       " {'VSAFETY2_-', 'VSEAT_9'},\n",
       " {'VSAFETY1_-', 'VSAFETY2_-'},\n",
       " 'VSAFETY2_-',\n",
       " {'VEJECTED_3', 'VSAFETY2_-'},\n",
       " {'PTYPE_2', 'VSAFETY2_-'},\n",
       " {'PSAFETY1_-', 'VSAFETY2_-'},\n",
       " {'PSAFETY2_-', 'VSAFETY2_-'},\n",
       " {'INSURED_O', 'VSAFETY2_-'},\n",
       " {'VEHYEAR_2012', 'VSAFETY2_-'},\n",
       " {'VEHMAKE_-', 'VSAFETY2_-'},\n",
       " {'VEHTYPE_N', 'VSAFETY2_-'},\n",
       " {'CHPTYPE1_60', 'VSAFETY2_-'},\n",
       " {'CRASHTYP_G', 'VSAFETY2_-'},\n",
       " {'INVOLVE_B', 'VSAFETY2_-'},\n",
       " {'VEJECTED_1', 'VSEAT_1'},\n",
       " 'VEJECTED_1',\n",
       " {'VEJECTED_3', 'VTYPE_3'},\n",
       " {'VEJECTED_3', 'VSEAT_9'},\n",
       " {'VEJECTED_3', 'VSAFETY1_-'},\n",
       " {'VEJECTED_3', 'VSAFETY2_-'},\n",
       " 'VEJECTED_3',\n",
       " {'PTYPE_2', 'VEJECTED_3'},\n",
       " {'PSAFETY1_-', 'VEJECTED_3'},\n",
       " {'PSAFETY2_-', 'VEJECTED_3'},\n",
       " {'INSURED_O', 'VEJECTED_3'},\n",
       " {'VEHYEAR_2012', 'VEJECTED_3'},\n",
       " {'VEHMAKE_-', 'VEJECTED_3'},\n",
       " {'VEHTYPE_N', 'VEJECTED_3'},\n",
       " {'CRASHTYP_G', 'VEJECTED_3'},\n",
       " {'INVOLVE_B', 'VEJECTED_3'},\n",
       " {'PTYPE_2', 'VTYPE_3'},\n",
       " {'PTYPE_2', 'VSEAT_9'},\n",
       " {'PTYPE_2', 'VSAFETY1_-'},\n",
       " {'PTYPE_2', 'VSAFETY2_-'},\n",
       " {'PTYPE_2', 'VEJECTED_3'},\n",
       " 'PTYPE_2',\n",
       " {'PSAFETY1_-', 'PTYPE_2'},\n",
       " {'PSAFETY2_-', 'PTYPE_2'},\n",
       " {'INSURED_O', 'PTYPE_2'},\n",
       " {'PTYPE_2', 'VEHYEAR_2012'},\n",
       " {'PTYPE_2', 'VEHMAKE_-'},\n",
       " {'PTYPE_2', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'PTYPE_2'},\n",
       " {'CRASHTYP_G', 'PTYPE_2'},\n",
       " {'INVOLVE_B', 'PTYPE_2'},\n",
       " 'PAGE_6',\n",
       " 'PSOBER_B',\n",
       " 'PDRUG_E',\n",
       " {'PSAFETY1_-', 'VTYPE_3'},\n",
       " {'PSAFETY1_-', 'VSEAT_9'},\n",
       " {'PSAFETY1_-', 'VSAFETY1_-'},\n",
       " {'PSAFETY1_-', 'VSAFETY2_-'},\n",
       " {'PSAFETY1_-', 'VEJECTED_3'},\n",
       " {'PSAFETY1_-', 'PTYPE_2'},\n",
       " 'PSAFETY1_-',\n",
       " {'PSAFETY1_-', 'PSAFETY2_-'},\n",
       " {'INSURED_O', 'PSAFETY1_-'},\n",
       " {'PSAFETY1_-', 'VEHYEAR_2012'},\n",
       " {'PSAFETY1_-', 'VEHMAKE_-'},\n",
       " {'PSAFETY1_-', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'PSAFETY1_-'},\n",
       " {'CRASHTYP_G', 'PSAFETY1_-'},\n",
       " {'INVOLVE_B', 'PSAFETY1_-'},\n",
       " 'PSAFETY1_P',\n",
       " {'PSAFETY2_-', 'VTYPE_3'},\n",
       " {'PSAFETY2_-', 'VSEAT_9'},\n",
       " {'PSAFETY2_-', 'VSAFETY1_-'},\n",
       " {'PSAFETY2_-', 'VSAFETY2_-'},\n",
       " {'PSAFETY2_-', 'VEJECTED_3'},\n",
       " {'PSAFETY2_-', 'PTYPE_2'},\n",
       " {'PSAFETY1_-', 'PSAFETY2_-'},\n",
       " 'PSAFETY2_-',\n",
       " {'INSURED_O', 'PSAFETY2_-'},\n",
       " {'PSAFETY2_-', 'VEHYEAR_2012'},\n",
       " {'PSAFETY2_-', 'VEHMAKE_-'},\n",
       " {'PSAFETY2_-', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'PSAFETY2_-'},\n",
       " {'CRASHTYP_G', 'PSAFETY2_-'},\n",
       " {'INVOLVE_B', 'PSAFETY2_-'},\n",
       " 'INSURED_N',\n",
       " {'INSURED_O', 'VTYPE_3'},\n",
       " {'INSURED_O', 'VSEAT_9'},\n",
       " {'INSURED_O', 'VSAFETY1_-'},\n",
       " {'INSURED_O', 'VSAFETY2_-'},\n",
       " {'INSURED_O', 'VEJECTED_3'},\n",
       " {'INSURED_O', 'PTYPE_2'},\n",
       " {'INSURED_O', 'PSAFETY1_-'},\n",
       " {'INSURED_O', 'PSAFETY2_-'},\n",
       " 'INSURED_O',\n",
       " {'INSURED_O', 'VEHYEAR_2012'},\n",
       " {'INSURED_O', 'VEHMAKE_-'},\n",
       " {'INSURED_O', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'INSURED_O'},\n",
       " {'CRASHTYP_G', 'INSURED_O'},\n",
       " {'INSURED_O', 'INVOLVE_B'},\n",
       " 'CELL_-',\n",
       " 'PVIOLCOD_ ',\n",
       " {'OAF1_A', 'PVIOLCOD_ '},\n",
       " {'OAF1_A', 'PVIOLCOD_ '},\n",
       " 'OAF1_A',\n",
       " 'MOVEMENT_-',\n",
       " {'VEHYEAR_2012', 'VTYPE_3'},\n",
       " {'VEHYEAR_2012', 'VSEAT_9'},\n",
       " {'VEHYEAR_2012', 'VSAFETY1_-'},\n",
       " {'VEHYEAR_2012', 'VSAFETY2_-'},\n",
       " {'VEHYEAR_2012', 'VEJECTED_3'},\n",
       " {'PTYPE_2', 'VEHYEAR_2012'},\n",
       " {'PSAFETY1_-', 'VEHYEAR_2012'},\n",
       " {'PSAFETY2_-', 'VEHYEAR_2012'},\n",
       " {'INSURED_O', 'VEHYEAR_2012'},\n",
       " 'VEHYEAR_2012',\n",
       " {'VEHMAKE_-', 'VEHYEAR_2012'},\n",
       " {'VEHTYPE_N', 'VEHYEAR_2012'},\n",
       " {'CHPTYPE1_60', 'VEHYEAR_2012'},\n",
       " {'CRASHTYP_G', 'VEHYEAR_2012'},\n",
       " {'INVOLVE_B', 'VEHYEAR_2012'},\n",
       " {'VEHMAKE_-', 'VTYPE_3'},\n",
       " {'VEHMAKE_-', 'VSEAT_9'},\n",
       " {'VEHMAKE_-', 'VSAFETY1_-'},\n",
       " {'VEHMAKE_-', 'VSAFETY2_-'},\n",
       " {'VEHMAKE_-', 'VEJECTED_3'},\n",
       " {'PTYPE_2', 'VEHMAKE_-'},\n",
       " {'PSAFETY1_-', 'VEHMAKE_-'},\n",
       " {'PSAFETY2_-', 'VEHMAKE_-'},\n",
       " {'INSURED_O', 'VEHMAKE_-'},\n",
       " {'VEHMAKE_-', 'VEHYEAR_2012'},\n",
       " 'VEHMAKE_-',\n",
       " {'VEHMAKE_-', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'VEHMAKE_-'},\n",
       " {'CRASHTYP_G', 'VEHMAKE_-'},\n",
       " {'INVOLVE_B', 'VEHMAKE_-'},\n",
       " {'VEHTYPE_N', 'VTYPE_3'},\n",
       " {'VEHTYPE_N', 'VSEAT_9'},\n",
       " {'VEHTYPE_N', 'VSAFETY1_-'},\n",
       " {'VEHTYPE_N', 'VSAFETY2_-'},\n",
       " {'VEHTYPE_N', 'VEJECTED_3'},\n",
       " {'PTYPE_2', 'VEHTYPE_N'},\n",
       " {'PSAFETY1_-', 'VEHTYPE_N'},\n",
       " {'PSAFETY2_-', 'VEHTYPE_N'},\n",
       " {'INSURED_O', 'VEHTYPE_N'},\n",
       " {'VEHTYPE_N', 'VEHYEAR_2012'},\n",
       " {'VEHMAKE_-', 'VEHTYPE_N'},\n",
       " 'VEHTYPE_N',\n",
       " {'CHPTYPE1_60', 'VEHTYPE_N'},\n",
       " {'CRASHTYP_G', 'VEHTYPE_N'},\n",
       " {'INVOLVE_B', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'VTYPE_3'},\n",
       " {'CHPTYPE1_60', 'VSEAT_9'},\n",
       " {'CHPTYPE1_60', 'VSAFETY1_-'},\n",
       " {'CHPTYPE1_60', 'VSAFETY2_-'},\n",
       " {'CHPTYPE1_60', 'PTYPE_2'},\n",
       " {'CHPTYPE1_60', 'PSAFETY1_-'},\n",
       " {'CHPTYPE1_60', 'PSAFETY2_-'},\n",
       " {'CHPTYPE1_60', 'INSURED_O'},\n",
       " {'CHPTYPE1_60', 'VEHYEAR_2012'},\n",
       " {'CHPTYPE1_60', 'VEHMAKE_-'},\n",
       " {'CHPTYPE1_60', 'VEHTYPE_N'},\n",
       " 'CHPTYPE1_60',\n",
       " {'CHPTYPE1_60', 'CRASHTYP_G'},\n",
       " {'CHPTYPE1_60', 'INVOLVE_B'},\n",
       " 'TIMECAT_300',\n",
       " 'CRASHTYP_E',\n",
       " {'CRASHTYP_E', 'INVOLVE_I'},\n",
       " {'CRASHTYP_G', 'VTYPE_3'},\n",
       " {'CRASHTYP_G', 'VSEAT_9'},\n",
       " {'CRASHTYP_G', 'VSAFETY1_-'},\n",
       " {'CRASHTYP_G', 'VSAFETY2_-'},\n",
       " {'CRASHTYP_G', 'VEJECTED_3'},\n",
       " {'CRASHTYP_G', 'PTYPE_2'},\n",
       " {'CRASHTYP_G', 'PSAFETY1_-'},\n",
       " {'CRASHTYP_G', 'PSAFETY2_-'},\n",
       " {'CRASHTYP_G', 'INSURED_O'},\n",
       " {'CRASHTYP_G', 'VEHYEAR_2012'},\n",
       " {'CRASHTYP_G', 'VEHMAKE_-'},\n",
       " {'CRASHTYP_G', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'CRASHTYP_G'},\n",
       " 'CRASHTYP_G',\n",
       " {'CRASHTYP_G', 'INVOLVE_B'},\n",
       " {'INVOLVE_B', 'VTYPE_3'},\n",
       " {'INVOLVE_B', 'VSEAT_9'},\n",
       " {'INVOLVE_B', 'VSAFETY1_-'},\n",
       " {'INVOLVE_B', 'VSAFETY2_-'},\n",
       " {'INVOLVE_B', 'VEJECTED_3'},\n",
       " {'INVOLVE_B', 'PTYPE_2'},\n",
       " {'INVOLVE_B', 'PSAFETY1_-'},\n",
       " {'INVOLVE_B', 'PSAFETY2_-'},\n",
       " {'INSURED_O', 'INVOLVE_B'},\n",
       " {'INVOLVE_B', 'VEHYEAR_2012'},\n",
       " {'INVOLVE_B', 'VEHMAKE_-'},\n",
       " {'INVOLVE_B', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'INVOLVE_B'},\n",
       " {'CRASHTYP_G', 'INVOLVE_B'},\n",
       " 'INVOLVE_B',\n",
       " {'CRASHTYP_E', 'INVOLVE_I'},\n",
       " 'INVOLVE_I',\n",
       " 'POP_9']"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(df.columns.values.tolist()[:-1])\n",
    "#组合AB特征\n",
    "list_A_B = []\n",
    "for A in df.columns.values.tolist()[:-1]:\n",
    "    for B in df.columns.values.tolist()[:-1]:\n",
    "        n_AB = df['Label'].groupby([df[A],df[B]]).count()\n",
    "        n_AB_and_label = df['Label'].groupby([df['Label'],df[A],df[B]]).count()\n",
    "        \n",
    "        if 2 in (n_AB.index.labels[0] + n_AB.index.labels[1]): #是否存在A为1，B也为1\n",
    "            if n_AB[1][1] > df.shape[0] * SUPPORT:  #AB满足支持度\n",
    "                 #是否存在Label为1，A也为1, B也为1， 即A,B,Label共存\n",
    "                if 3 in (n_AB_and_label.index.labels[0] + n_AB_and_label.index.labels[1] +n_AB_and_label.index.labels[2]): \n",
    "                    if n_AB_and_label[1][1][1]/n_AB[1][1] > CONFIDENCE:#num(A,B,label)/num(A,B) > CONFIDENCE(满足置信度)\n",
    "                        if A == B:\n",
    "                            list_A_B.append(A) \n",
    "                        else:\n",
    "                            list_A_B.append({A,B})    \n",
    "\n",
    "list_A_B                      \n",
    "# print(n_AB_and_label.index)\n",
    "# n_AB_and_label  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 列表去重便是最终结果了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['VTYPE_1',\n",
       " {'VSEAT_1', 'VTYPE_1'},\n",
       " 'VTYPE_3',\n",
       " {'VSEAT_9', 'VTYPE_3'},\n",
       " {'VSAFETY1_-', 'VTYPE_3'},\n",
       " {'VSAFETY2_-', 'VTYPE_3'},\n",
       " {'VEJECTED_3', 'VTYPE_3'},\n",
       " {'PTYPE_2', 'VTYPE_3'},\n",
       " {'PSAFETY1_-', 'VTYPE_3'},\n",
       " {'PSAFETY2_-', 'VTYPE_3'},\n",
       " {'INSURED_O', 'VTYPE_3'},\n",
       " {'VEHYEAR_2012', 'VTYPE_3'},\n",
       " {'VEHMAKE_-', 'VTYPE_3'},\n",
       " {'VEHTYPE_N', 'VTYPE_3'},\n",
       " {'CHPTYPE1_60', 'VTYPE_3'},\n",
       " {'CRASHTYP_G', 'VTYPE_3'},\n",
       " {'INVOLVE_B', 'VTYPE_3'},\n",
       " 'VSEAT_1',\n",
       " {'VEJECTED_1', 'VSEAT_1'},\n",
       " 'VSEAT_9',\n",
       " {'VSAFETY1_-', 'VSEAT_9'},\n",
       " {'VSAFETY2_-', 'VSEAT_9'},\n",
       " {'VEJECTED_3', 'VSEAT_9'},\n",
       " {'PTYPE_2', 'VSEAT_9'},\n",
       " {'PSAFETY1_-', 'VSEAT_9'},\n",
       " {'PSAFETY2_-', 'VSEAT_9'},\n",
       " {'INSURED_O', 'VSEAT_9'},\n",
       " {'VEHYEAR_2012', 'VSEAT_9'},\n",
       " {'VEHMAKE_-', 'VSEAT_9'},\n",
       " {'VEHTYPE_N', 'VSEAT_9'},\n",
       " {'CHPTYPE1_60', 'VSEAT_9'},\n",
       " {'CRASHTYP_G', 'VSEAT_9'},\n",
       " {'INVOLVE_B', 'VSEAT_9'},\n",
       " 'VSAFETY1_-',\n",
       " {'VSAFETY1_-', 'VSAFETY2_-'},\n",
       " {'VEJECTED_3', 'VSAFETY1_-'},\n",
       " {'PTYPE_2', 'VSAFETY1_-'},\n",
       " {'PSAFETY1_-', 'VSAFETY1_-'},\n",
       " {'PSAFETY2_-', 'VSAFETY1_-'},\n",
       " {'INSURED_O', 'VSAFETY1_-'},\n",
       " {'VEHYEAR_2012', 'VSAFETY1_-'},\n",
       " {'VEHMAKE_-', 'VSAFETY1_-'},\n",
       " {'VEHTYPE_N', 'VSAFETY1_-'},\n",
       " {'CHPTYPE1_60', 'VSAFETY1_-'},\n",
       " {'CRASHTYP_G', 'VSAFETY1_-'},\n",
       " {'INVOLVE_B', 'VSAFETY1_-'},\n",
       " 'VSAFETY1_L',\n",
       " 'VSAFETY2_-',\n",
       " {'VEJECTED_3', 'VSAFETY2_-'},\n",
       " {'PTYPE_2', 'VSAFETY2_-'},\n",
       " {'PSAFETY1_-', 'VSAFETY2_-'},\n",
       " {'PSAFETY2_-', 'VSAFETY2_-'},\n",
       " {'INSURED_O', 'VSAFETY2_-'},\n",
       " {'VEHYEAR_2012', 'VSAFETY2_-'},\n",
       " {'VEHMAKE_-', 'VSAFETY2_-'},\n",
       " {'VEHTYPE_N', 'VSAFETY2_-'},\n",
       " {'CHPTYPE1_60', 'VSAFETY2_-'},\n",
       " {'CRASHTYP_G', 'VSAFETY2_-'},\n",
       " {'INVOLVE_B', 'VSAFETY2_-'},\n",
       " 'VEJECTED_1',\n",
       " 'VEJECTED_3',\n",
       " {'PTYPE_2', 'VEJECTED_3'},\n",
       " {'PSAFETY1_-', 'VEJECTED_3'},\n",
       " {'PSAFETY2_-', 'VEJECTED_3'},\n",
       " {'INSURED_O', 'VEJECTED_3'},\n",
       " {'VEHYEAR_2012', 'VEJECTED_3'},\n",
       " {'VEHMAKE_-', 'VEJECTED_3'},\n",
       " {'VEHTYPE_N', 'VEJECTED_3'},\n",
       " {'CRASHTYP_G', 'VEJECTED_3'},\n",
       " {'INVOLVE_B', 'VEJECTED_3'},\n",
       " 'PTYPE_2',\n",
       " {'PSAFETY1_-', 'PTYPE_2'},\n",
       " {'PSAFETY2_-', 'PTYPE_2'},\n",
       " {'INSURED_O', 'PTYPE_2'},\n",
       " {'PTYPE_2', 'VEHYEAR_2012'},\n",
       " {'PTYPE_2', 'VEHMAKE_-'},\n",
       " {'PTYPE_2', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'PTYPE_2'},\n",
       " {'CRASHTYP_G', 'PTYPE_2'},\n",
       " {'INVOLVE_B', 'PTYPE_2'},\n",
       " 'PAGE_6',\n",
       " 'PSOBER_B',\n",
       " 'PDRUG_E',\n",
       " 'PSAFETY1_-',\n",
       " {'PSAFETY1_-', 'PSAFETY2_-'},\n",
       " {'INSURED_O', 'PSAFETY1_-'},\n",
       " {'PSAFETY1_-', 'VEHYEAR_2012'},\n",
       " {'PSAFETY1_-', 'VEHMAKE_-'},\n",
       " {'PSAFETY1_-', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'PSAFETY1_-'},\n",
       " {'CRASHTYP_G', 'PSAFETY1_-'},\n",
       " {'INVOLVE_B', 'PSAFETY1_-'},\n",
       " 'PSAFETY1_P',\n",
       " 'PSAFETY2_-',\n",
       " {'INSURED_O', 'PSAFETY2_-'},\n",
       " {'PSAFETY2_-', 'VEHYEAR_2012'},\n",
       " {'PSAFETY2_-', 'VEHMAKE_-'},\n",
       " {'PSAFETY2_-', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'PSAFETY2_-'},\n",
       " {'CRASHTYP_G', 'PSAFETY2_-'},\n",
       " {'INVOLVE_B', 'PSAFETY2_-'},\n",
       " 'INSURED_N',\n",
       " 'INSURED_O',\n",
       " {'INSURED_O', 'VEHYEAR_2012'},\n",
       " {'INSURED_O', 'VEHMAKE_-'},\n",
       " {'INSURED_O', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'INSURED_O'},\n",
       " {'CRASHTYP_G', 'INSURED_O'},\n",
       " {'INSURED_O', 'INVOLVE_B'},\n",
       " 'CELL_-',\n",
       " 'PVIOLCOD_ ',\n",
       " {'OAF1_A', 'PVIOLCOD_ '},\n",
       " 'OAF1_A',\n",
       " 'MOVEMENT_-',\n",
       " 'VEHYEAR_2012',\n",
       " {'VEHMAKE_-', 'VEHYEAR_2012'},\n",
       " {'VEHTYPE_N', 'VEHYEAR_2012'},\n",
       " {'CHPTYPE1_60', 'VEHYEAR_2012'},\n",
       " {'CRASHTYP_G', 'VEHYEAR_2012'},\n",
       " {'INVOLVE_B', 'VEHYEAR_2012'},\n",
       " 'VEHMAKE_-',\n",
       " {'VEHMAKE_-', 'VEHTYPE_N'},\n",
       " {'CHPTYPE1_60', 'VEHMAKE_-'},\n",
       " {'CRASHTYP_G', 'VEHMAKE_-'},\n",
       " {'INVOLVE_B', 'VEHMAKE_-'},\n",
       " 'VEHTYPE_N',\n",
       " {'CHPTYPE1_60', 'VEHTYPE_N'},\n",
       " {'CRASHTYP_G', 'VEHTYPE_N'},\n",
       " {'INVOLVE_B', 'VEHTYPE_N'},\n",
       " 'CHPTYPE1_60',\n",
       " {'CHPTYPE1_60', 'CRASHTYP_G'},\n",
       " {'CHPTYPE1_60', 'INVOLVE_B'},\n",
       " 'TIMECAT_300',\n",
       " 'CRASHTYP_E',\n",
       " {'CRASHTYP_E', 'INVOLVE_I'},\n",
       " 'CRASHTYP_G',\n",
       " {'CRASHTYP_G', 'INVOLVE_B'},\n",
       " 'INVOLVE_B',\n",
       " 'INVOLVE_I',\n",
       " 'POP_9']"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#list_A_B=list(set(list_A_B))\n",
    "\n",
    "result=[]\n",
    "for i in list_A_B:\n",
    "    if i not in result:\n",
    "        result.append(i)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获取 每行中为1的特征， 收集到列表里， 转化后数据类型对应pyfpgrowth接受类型\n",
    "# dfT = df.T\n",
    "# data = [dfT[(dfT[i]==1)].index.tolist() for i in range(df.shape[0])]\n",
    "# print(data[0])\n",
    "# #用pyfpgrowth训练\n",
    "# import pyfpgrowth\n",
    "# patterns = pyfpgrowth.find_frequent_patterns(data, df.shape[0] * 0.1)\n",
    "## 根据置信度生成规则\n",
    "# rules = pyfpgrowth.generate_association_rules(patterns, 0.7)\n",
    "# rules\n",
    "## 从规则中过滤出自己需要的规则\n",
    "## 解析规则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## 谢谢您的观看！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
